package com.zwps.common.tool.ffmpeg;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.zwps.common.tool.shell.ShellProcessTool;

public class FFmpegTool {

    public static void main(String[] args) throws Exception {
//        makeScreenCut("/usr/bin/ffmpeg", "http://171.34.76.171:8880//statics/cache/%E4%BA%94%E6%98%9F%E7%99%BD%E9%B9%A4%E4%BF%9D%E6%8A%A4%E5%B0%8F%E5%8C%BA%E5%8F%AF%E8%A7%81%E5%85%89_2022-03-01_15-56-47.mp4", "/home/lyh/temp/test.jpg");
        makeScreenCut("/usr/bin/ffmpeg",
                "http://223.83.101.206:8000/thl_data/红外相机原始/黄花/2020年红外相机/六山口/六山口1/Ere 0016.MP4",
                "/home/lyh/temp/test2.jpg");
//        makeScreenCut("/usr/bin/ffmpeg", "/home/lyh/Downloads/五星白鹤保护小区可见光_2022-03-28_09-39-12视频.mp4", "/home/lyh/temp/test.jpg");
    }

    public static void main2(String[] args) throws Exception {
        String ffmpegPath = "/usr/bin/ffmpeg";
//        String mp4Url = "http://171.34.76.171:8880//statics/cache/%E4%BA%94%E6%98%9F%E7%99%BD%E9%B9%A4%E4%BF%9D%E6%8A%A4%E5%B0%8F%E5%8C%BA%E5%8F%AF%E8%A7%81%E5%85%89_2022-03-01_15-56-47.mp4";
        String mp4Url = "http://223.83.101.206:8000/thl_data/红外相机原始/黄花/2020年红外相机/六山口/六山口1/Ere 0016.MP4";
        MultimediaInfo info = parseVideoInfo(ffmpegPath, mp4Url);
        // 时长信息
        long duration = info.getDuration();
        System.out.println("视频时长为：" + duration / 1000 + "秒");
        // 音频信息
        MultimediaInfo.AudioInfo audio = info.getAudio();
        if (audio != null) {
            int bitRate = audio.getBitRate(); // 比特率
            int channels = audio.getChannels(); // 声道
            String decoder = audio.getDecoder(); // 解码器
            int sRate = audio.getSamplingRate(); // 采样率
            System.out.println("解码器：" + decoder + "，声道：" + channels + "，比特率：" + bitRate + "，采样率：" + sRate);
        }
        // 视频信息
        MultimediaInfo.VideoInfo video = info.getVideo();
        if (video != null) {
            int bitRate2 = video.getBitRate();
            Float fRate = video.getFrameRate(); // 帧率
            int height = video.getHeight(); // 视频高度
            int width = video.getWidth(); // 视频宽度
            System.out.println("视频帧率：" + fRate + "，比特率：" + bitRate2 + "，视频高度：" + height + "，视频宽度：" + width);
        }
    }

    public static MultimediaInfo parseVideoInfo(String ffmepgPath, String videoRealPath) throws Exception {
        // 使用ProcessBuilder执行的shell 命令不需要添加空格间隔符
        ArrayList<String> command = new ArrayList<String>();
        command.add(ffmepgPath);
        command.add("-i");
        command.add(videoRealPath);
        command.add("-f");
        command.add("null");
        command.add("-");
        List<String> printLines = ShellProcessTool.executeCmd(command);
        return FfmpegParseVideoTool.parseMultimediaInfo(printLines);
    }

    /**
     * 
     * @Title: makeScreenCut
     * @Description: 获取第一秒的图片
     * @param ffmepgPath
     * @param videoRealPath
     * @param imageRealName
     * @throws IOException void
     * @date 2022年11月28日
     */
    public static void makeScreenCut(String ffmepgPath, String videoRealPath, String imageRealName, Integer width,
            Integer height) throws Exception {
        ArrayList<String> command = new ArrayList<String>();
        command.add(ffmepgPath);
        command.add("-i");
        command.add(videoRealPath);
        command.add("-y");
        command.add("-f");
        command.add("image2");
        command.add("-ss");
        command.add("0");
        command.add("-t");
        command.add("0.001");
        if (width != null && width > 0 && height != null && height > 0) {
            command.add("-s");
            command.add(width.toString());
            command.add("x");
            command.add(height.toString());
        }
        command.add(imageRealName);
        ShellProcessTool.executeCmd(command);
    }

    public static void makeScreenCut(String ffmepgPath, String videoRealPath, String imageRealName) throws Exception {
        makeScreenCut(ffmepgPath, videoRealPath, imageRealName, null, null);
    }
}